/**
 * Created by liubin on 2017/6/2.
 */



var DtjTablePlayer = GameBaseItem.extend({

    mSeat : null,
    Panel_anim : null,
    Image_head : null,
    Text_nick : null,
    Text_score : null,
    Image_ready : null,
    Image_host : null,
    Text_offline : null,
    Panel_chat : null,
    Panel_bet : null,
    Atlas_bet1 : null,
    Atlas_bet2 : null,
    Image_result : null,
    Image_makeHost : null,
    isHost : null,

    ctor: function (seat) {
        this._super();
        this.loadCocostudio("res/ccs/DtjTablePlayer.json");
        mo.gameHelper.fixIosText(this.ccsNode);
        this.mSeat = seat;
        this.isHost = false;

        this.Panel_anim = mo.seekWidgetByName(this.ccsNode, "Panel_anim");
        this.Image_head = mo.seekWidgetByName(this.ccsNode, "Image_head");
        this.Text_nick = mo.seekWidgetByName(this.ccsNode, "Text_nick");
        this.Text_score = mo.seekWidgetByName(this.ccsNode, "Text_score");
        this.Image_ready = mo.seekWidgetByName(this.ccsNode, "Image_ready");
        this.Image_host = mo.seekWidgetByName(this.ccsNode, "Image_host");
        this.Text_offline = mo.seekWidgetByName(this.ccsNode, "Text_offline");

        this.Image_result = mo.seekWidgetByName(this.ccsNode, "Image_result");
        this.Image_makeHost = mo.seekWidgetByName(this.ccsNode, "Image_makeHost");
        
        var Panel_chat = mo.seekWidgetByName(this.ccsNode, "Panel_chat");

        Panel_chat.setVisible(false);
        var Panel_chatFan = mo.seekWidgetByName(this.ccsNode, "Panel_chatFan");
        Panel_chatFan.setVisible(false);
        if (seat == 1 || seat == 2){
            this.Panel_chat = Panel_chatFan;
        }else{
            this.Panel_chat = Panel_chat;
        }

        var Panel_bet = mo.seekWidgetByName(this.ccsNode, "Panel_bet");
        var Panel_bet_fan = mo.seekWidgetByName(this.ccsNode, "Panel_bet_fan");
        var Panel_bet_xia = mo.seekWidgetByName(this.ccsNode, "Panel_bet_xia");
        Panel_bet.setVisible(false);
        Panel_bet_fan.setVisible(false);
        Panel_bet_xia.setVisible(false);

        if(seat == 0){
            this.Panel_bet = Panel_bet_fan;
        }else if(seat == 1 || seat == 3){
            this.Panel_bet = Panel_bet_xia;
        }else{
            this.Panel_bet = Panel_bet;
        }
        this.Atlas_bet1 = mo.seekWidgetByName(this.Panel_bet, "num1");
        this.Atlas_bet2 = mo.seekWidgetByName(this.Panel_bet, "num2");

        this.Panel_anim.setVisible(false);
        this.Image_ready.setVisible(false);
        this.Image_host.setVisible(false);
        this.Text_offline.setVisible(false);
        this.Panel_chat.setVisible(false);
        this.Panel_bet.setVisible(false);
        this.Image_result.setVisible(false);

        mo.seekWidgetByName(this.ccsNode, "Image_player").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                if (mo.dtjTable.mPlayerType == 0){
                    return;
                }

                var index = seat;
                var logicSeat = mo.dtjTableStatus.getLogicForView(index, mo.dtjTableStatus.mTableStatus.MySeat - 1) + 1;
                var seatUser = mo.dtjTableStatus.mTableStatus["SeatUser" + logicSeat];
                if (seatUser != null){
                    mo.dtjTable.mDtjTableUILayer.mDtjTablePlayerInfoLayer.showPlayerInfo(index, seatUser.user_info, mo.dtjTable.mDtjTableUILayer.mPlayerPoss[index], logicSeat - 1);
                }
            }
        }, this);
    },

    showAddScoreAnim : function(score, delayTime){
        var that = this;

        this.scheduleOnce(function(dt){
            this.playResultAni(score);

            this.Panel_anim.setVisible(true);

            var Image_animBg0 = mo.seekWidgetByName(this.Panel_anim, "Image_animBg0");
            var Image_animBg1 = mo.seekWidgetByName(this.Panel_anim, "Image_animBg1");
            var Image_animBg2 = mo.seekWidgetByName(this.Panel_anim, "Image_animBg2");
            var BitmapFontLabel_add = mo.seekWidgetByName(this.Panel_anim, "BitmapFontLabel_add");
            var BitmapFontLabel_sub = mo.seekWidgetByName(this.Panel_anim, "BitmapFontLabel_sub");

            Image_animBg0.setVisible(false);
            Image_animBg1.setVisible(false);
            Image_animBg2.setVisible(false);
            BitmapFontLabel_add.setVisible(false);
            BitmapFontLabel_sub.setVisible(false);

            if (score > 0){
                var text = score >= 0 ? BitmapFontLabel_add : BitmapFontLabel_sub;

                //分数
                text.setVisible(true);
                text.setOpacity(100);
                text.setPosition(cc.p(0, 0));
                text.setString("+s" + score);
                text.runAction(cc.sequence(
                    cc.spawn(
                        cc.moveTo(0.2, cc.p(0, 80)),
                        cc.fadeIn(0.2)
                    ),
                    cc.callFunc(function(){
                        //改变分数
                        var currentScore = parseInt(that.Text_score.getString());
                        currentScore += score;
                        that.Text_score.setString(currentScore + "");

                        if(mo.dtjTable.mGameConfig.guo == 1 && that.isHost){
                            var guo_score = mo.seekWidgetByName(mo.dtjTable.mDtjTableUILayer.ccsNode, "guodiNum");
                            guo_score.ignoreContentAdaptWithSize(true);
                            guo_score.setString(mo.dtjTableStatus.mTableStatus.GuoPoint + "");
                        }
                    }),
                    cc.delayTime(1),
                    cc.hide()
                ));

                //背景0
                Image_animBg0.setVisible(false);
                Image_animBg0.runAction(cc.sequence(
                    cc.delayTime(0.2),
                    cc.show(),
                    cc.delayTime(0.4),
                    cc.hide()
                ));

                //背景0
                Image_animBg1.setVisible(false);
                Image_animBg1.setScale(1.0);
                Image_animBg1.setOpacity(255);
                Image_animBg1.runAction(cc.sequence(
                    cc.show(),
                    cc.spawn(
                        cc.scaleTo(0.4, 1.2),
                        cc.fadeOut(0.4)
                    )
                ));

                //背景0
                Image_animBg2.setVisible(false);
                Image_animBg2.setScale(1.0);
                Image_animBg2.setOpacity(255);
                Image_animBg2.runAction(cc.sequence(
                    cc.delayTime(0.3),
                    cc.show(),
                    cc.spawn(
                        cc.scaleTo(0.4, 1.4),
                        cc.fadeOut(0.4)
                    )
                ));
            }else{
                var text = score >= 0 ? BitmapFontLabel_add : BitmapFontLabel_sub;

                //分数
                text.setVisible(true);
                text.setOpacity(100);
                text.setPosition(cc.p(0, 0));
                text.setString("" + score);
                text.runAction(cc.sequence(
                    cc.spawn(
                        cc.moveTo(0.2, cc.p(0, 80)),
                        cc.fadeIn(0.2)
                    ),
                    cc.callFunc(function(){
                        //改变分数
                        var currentScore = parseInt(that.Text_score.getString());
                        currentScore += score;
                        that.Text_score.setString(currentScore + "");

                        if(mo.dtjTable.mGameConfig.guo == 1 && that.isHost){
                            var guo_score = mo.seekWidgetByName(mo.dtjTable.mDtjTableUILayer.ccsNode, "guodiNum");
                            guo_score.ignoreContentAdaptWithSize(true);
                            guo_score.setString(mo.dtjTableStatus.mTableStatus.GuoPoint + "");
                        }

                    }),
                    cc.delayTime(1),
                    cc.hide()
                ));
            }
        }, delayTime);
    },

    setPlayerInfo : function(info){
        if (info != null){
            this.setVisible(true);

            this.isHost = info.IsHost;

            //昵称
            mo.gameHelper.setTextString(this.Text_nick, mo.gameHelper.getUserNickFilter(info.user_info.nickname));

            //头像
            mo.gameHelper.loadImageForUrl(this.Image_head, info.user_info.faceUrl);


            //分数
            if(mo.dtjTable.mGameConfig.guo == 1 && info.IsHost) {
                var guo_score = mo.seekWidgetByName(mo.dtjTable.mDtjTableUILayer.ccsNode, "guodiNum");
                guo_score.setVisible(true);

                if (mo.dtjTableStatus.mTableStatus.Step == 0) {
                    if (this.Text_score.getString() == "9999") {
                        guo_score.ignoreContentAdaptWithSize(true);
                        guo_score.setString(mo.dtjTableStatus.mTableStatus.GuoPoint + "");
                    } else {
                        guo_score.runAction(cc.sequence(
                            cc.delayTime(5),
                            cc.callFunc(function () {
                                guo_score.ignoreContentAdaptWithSize(true);
                                guo_score.setString(mo.dtjTableStatus.mTableStatus.GuoPoint + "");
                            }, guo_score)
                        ));
                    }

                }else{
                    guo_score.ignoreContentAdaptWithSize(true);
                    guo_score.setString(mo.dtjTableStatus.mTableStatus.GuoPoint + "");
                }
            }

            this.Text_score.stopAllActions();
            if (mo.dtjTableStatus.mTableStatus.Step == 0) {
                if (this.Text_score.getString() == "-999") {
                    mo.gameHelper.setTextString(this.Text_score, info.Score + "");
                } else {
                    this.Text_score.runAction(cc.sequence(
                        cc.delayTime(10),
                        cc.callFunc(function () {
                            mo.gameHelper.setTextString(this, info.Score + "");
                        }, this.Text_score)
                    ));
                }
            } else {
                mo.gameHelper.setTextString(this.Text_score, info.Score + "");
            }

            //准备状态
            if (mo.dtjTableStatus.mTableStatus.Step == 0 && info.State == 1){
                this.Image_ready.setVisible(true);

                this.Image_result.setVisible(false);
            }else{
                this.Image_ready.setVisible(false);
            }

            if(mo.dtjTableStatus.mTableStatus.Step == 3 || mo.dtjTableStatus.mTableStatus.Step == 4) {
                if (info.AddRate1 > 0 && !info.IsHost){
                    this.Panel_bet.setVisible(true);

                    var beiNum1 = info.AddRate1;
                    var beiNum2 = info.AddRate2;

                    this.Atlas_bet1.ignoreContentAdaptWithSize(true);
                    this.Atlas_bet2.ignoreContentAdaptWithSize(true);

                    this.Atlas_bet1.setString(beiNum1 + "");
                    this.Atlas_bet2.setString(beiNum2 + "");
                }else{
                    this.Panel_bet.setVisible(false);
                }
            }

            //抢庄
            if (mo.dtjTableStatus.mTableStatus.Step == 2){
                mo.dtjTable.mDtjTableUILayer.showMakeHostImage(this.mSeat, info.MakeHostRate);
            }else{
                mo.dtjTable.mDtjTableUILayer.showMakeHostImage(this.mSeat, -1);
            }

            //庄家
            if (mo.dtjTableStatus.mTableStatus.Step != 2 && info.IsHost) {
                this.Image_host.setVisible(true);
            } else {
                this.Image_host.setVisible(false);
            }

        }else{
            this.setVisible(false);
        }
    },

    playMakeHostAni : function () {
        this.Image_makeHost.runAction(cc.sequence(
            cc.show(),
            cc.delayTime(0.2),
            cc.hide()
        ));

    },

    playHostAni : function () {
        this.Image_host.setScale(5);
        this.Image_host.setVisible(true);

        this.Image_host.runAction(cc.scaleTo(0.2, 1.0));
    },

    playResultAni : function (score) {
        if(score == 0){
            mo.gameHelper.loadTextureImage(this.Image_result, "res/ccs/dtj/result_3.png");
        }else if(score > 0){
            mo.gameHelper.loadTextureImage(this.Image_result, "res/ccs/dtj/result_2.png");
        }else{
            mo.gameHelper.loadTextureImage(this.Image_result, "res/ccs/dtj/result_1.png");
        }

        this.Image_result.setScale(5);
        this.Image_result.setVisible(true);

        this.Image_result.runAction(cc.scaleTo(0.2, 1.0));
    },

    setOnlineStatus : function(isOffline){
        this.Text_offline.setVisible(isOffline);
    }
});